package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理
 */
@RestController
@RequestMapping("/admin/category")
@Api(tags = "分类相关接口")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 分类分页查询
     *
//     * @param name
//     * @param page
//     * @param pageSize
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> pageList(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageResult pageResult = categoryService.pageList(categoryPageQueryDTO);

        return Result.success(pageResult);
    }

    /**
     * 根据类型查询分类
     *
     * @param type
     * @return
     */
    @GetMapping("/list")
    public Result<List<Category>> list(Integer type) {
        List<Category> list = categoryService.list(type);

        return Result.success(list);
    }

    /**
     * 启用、禁用分类
     *
     * @param status
     * @param categoryDTO
     * @return
     */
    @PostMapping("/status/{status}")
    public Result<String> updateStatus(@PathVariable Integer status, CategoryDTO categoryDTO) {
        categoryService.updateStatus(status, categoryDTO.getId());

        return Result.success();
    }

    /**
     * 根据id删除分类
     *
     * @param categoryDTO
     * @return
     */
    @DeleteMapping
    public Result<String> delete(CategoryDTO categoryDTO) {
        categoryService.delete(categoryDTO);

        return Result.success();
    }

    /**
     * 新增分类
     *
     * @param categoryDTO
     * @return
     */
    @PostMapping
    public Result<String> add(@RequestBody CategoryDTO categoryDTO) {
        categoryService.add(categoryDTO);

        return Result.success();
    }

    /**
     * 修改分类信息
     * @param categoryDTO
     * @return
     */
    @PutMapping
    public Result<String> updateInfo(@RequestBody CategoryDTO categoryDTO) {
        categoryService.updateInfo(categoryDTO);

        return Result.success();
    }
}
